package com.nuoniu.sibanyun.controller.crm;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.SendSms;
import com.nuoniu.sibanyun.entity.SysUser;
import com.nuoniu.sibanyun.entity.crm.CrmForm;
import com.nuoniu.sibanyun.entity.crm.CrmFormField;
import com.nuoniu.sibanyun.entity.crm.CrmInvitationForm;
import com.nuoniu.sibanyun.service.crm.ICrmFormFieldService;
import com.nuoniu.sibanyun.service.crm.ICrmFormService;
import com.nuoniu.sibanyun.service.crm.ICrmInvitationFormService;
import com.nuoniu.sibanyun.service.redis.RedisService;
import com.nuoniu.sibanyun.utils.IpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 邀约表单 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-09-03
 */
@RestController
@RequestMapping("/crm/invitationForm")
@Api(tags = "邀约表单管理")
public class CrmInvitationFormController extends BaseController {

    @Autowired
    private ICrmInvitationFormService invitationFormService;
    @Autowired
    private ICrmFormFieldService formFieldService;
    @Autowired
    private ICrmFormService formService;

    @ApiOperation("获取表单数据列表")
    @GetMapping("pageList")
    public HttpResult pageList(Page page, CrmInvitationForm invitationForm) {
        return HttpResult.ok(invitationFormService.pageList(page, invitationForm));
    }

    @ApiOperation("保存表单数据")
    @PostMapping("save")
    public HttpResult saveForm(@RequestBody CrmInvitationForm invitationForm) {
        if(invitationForm.getFormId() == null) {
            throw new NuoNiuException("提交时间已截止");
        }

        if(StringUtils.isBlank(invitationForm.getCrmName())) {
            throw new NuoNiuException("客户名称不能为空");
        }

        if(invitationForm.getShareUserId() == null) {
            throw new NuoNiuException("分享人不存在");
        }

        if(StringUtils.isBlank(invitationForm.getPhone()) || invitationForm.getPhone().length() != 11) {
            throw new NuoNiuException("请输入正确手机号");
        }

        invitationFormService.saveForm(invitationForm);
        return HttpResult.ok();
    }

    @ApiOperation("删除表单数据")
    @DeleteMapping("delete")
    public HttpResult deleteFormData(Long id) {
        invitationFormService.deleteFormData(id);
        return HttpResult.ok();
    }

    @ApiOperation("获取表单详情")
    @GetMapping("getDetail")
    public HttpResult getDetail(Long id) {
        return HttpResult.ok(invitationFormService.getDetail(id));
    }

    @ApiOperation("生成邀约链接")
    @GetMapping("getInvitationUrl")
    public HttpResult getInvitationUrl(Long id) {
        return HttpResult.ok(invitationFormService.getInvitationUrl(id));
    }

    @ApiOperation("获取表单字段列表")
    @GetMapping("getFormFieldList")
    public HttpResult getFormFieldList(Long formId) {
        return HttpResult.ok(formFieldService.getFormFieldList(formId));
    }

    /**
     * 保存表单
     * @author 小啊刚
     * @date 2021-09-07
     */
    @ApiOperation("保存表单")
    @PostMapping("saveCrmFiled")
    public HttpResult saveCrmFiled(@RequestBody CrmForm crmForm) {
        if(crmForm == null || crmForm.getFields().size() == 0) {
            throw new NuoNiuException("表单字段不能为空");
        }

        if(crmForm.getActivityId() == null) {
            throw new NuoNiuException("请选择活动");
        }

        formFieldService.saveCrmFiled(crmForm);
        return HttpResult.ok();
    }

    @ApiOperation("获取表单列表")
    @GetMapping("getFromList")
    public HttpResult getFromList(Page page, CrmForm form) {
        return HttpResult.ok(formService.pageList(page, form));
    }

    @ApiOperation("删除表单")
    @DeleteMapping("deleteForm")
    public HttpResult deleteForm(Long formId) {
        invitationFormService.deleteForm(formId);
        return HttpResult.ok();
    }

    @ApiOperation("发送短信验证码")
    @PostMapping("sendSms")
    public HttpResult sendSms(@RequestBody CrmInvitationForm invitationForm, HttpServletRequest request) {
        if(invitationForm == null || invitationForm.getFormId() == null) {
            throw new NuoNiuException("表单不能为空");
        }

        if(StringUtils.isBlank(invitationForm.getImgCode())) {
           throw new NuoNiuException("图片验证码不能为空");
        }

        String regex = "^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";
        if(StringUtils.isEmpty(invitationForm.getPhone()) || invitationForm.getPhone().length() != 11) {
            throw new NuoNiuException("手机号格式不正确");
        }

        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(invitationForm.getPhone());
        boolean isMatch = m.matches();
        if(!isMatch) {
            throw new NuoNiuException("手机号格式不正确");
        }

        invitationFormService.sendSms(invitationForm, request);
        return HttpResult.ok();
    }
}
